<template>
	<view class="page">
		<view class="_img flex justify-center align-center">
			<image src="https://sanfuzhide.oss-cn-hangzhou.aliyuncs.com/vip/logo.png" mode=""></image>
		</view>
		<view class="title flex justify-center align-center">
			欢迎使用山富职得!
		</view>
		<view v-if="!stauts" class="authorization">
			<button type="default" @click="authorization">
				<view class="botton">
					开启登录授权
				</view>
			</button>
		</view>
		<view v-if="stauts" class="authorization">
			<button type="default" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">
				<view class="botton">
					开启手机号授权
				</view>
			</button>
		</view>
		<view class="skip flex justify-center align-center" @click="close">
			<view class="skip-item">
				跳过
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				state: false,
				isCanUse: true,
				userinfo: {
					scene:''
				},
				stauts: false,
				time: 0,
				date: 0
			}
		},
		onLoad(e) {
			if(e.scene){
			let id = decodeURIComponent(e.scene)
			console.log(id)
			this.userinfo.scene = id
			}
		},
		onShow() {
			this.getCode()
		},
		methods: {
			//获取code
			getCode() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						uni.setStorageSync('code', res.code)
						this.userinfo.code = res.code
					}
				})
			},
			//跳过
			close() {
				uni.showModal({
					content: '跳过授权可能无法正常使用小程序!确定跳过吗?',
					success: (res) => {
						if (res.confirm) {
							uni.switchTab({
								url: '../circle/circle'
							})
						} else if (res.cancel) {
							return
						}
					}
				})
			},
			//登录授权
			authorization() { 
				if (uni.getStorageSync('openId')) {
					uni.switchTab({
						url: '../circle/circle'
					})
				} else {
					uni.getUserProfile({
						desc: '登录',
						success: (res) => {
							Object.keys(res.userInfo).forEach(item => {
								this.userinfo[item] = res.userInfo[item]
							})
							console.log(this.userinfo)
							uni.setStorageSync('avatar', res.userInfo.avatarUrl)
							uni.setStorageSync('nickName', res.userInfo.nickName)
							console.log(res)
							this.getList()
						},
						fail: (e) => {
							this.authorization()
						}
					})
				}

			},
			// 手机号授权
			async decryptPhoneNumber(e) {
				if(this.date == 1){
					return
				}
				console.log(e)
				this.date = 1
				let sessionKey = uni.getStorageSync('sessionKey')
				let openId = uni.getStorageSync('openId')
				let arr = {}
				arr.iv = e.detail.iv
				arr.sessionKey = sessionKey
				arr.encryptedData = e.detail.encryptedData
				let {
					data,code
				} = await this.$post('/backend/v1/userObtainPhone',arr)
				if(code == 200){
					this.$post('/user/updateUserInfo',{userPhone:data.phoneNumber})
					uni.switchTab({
						 url: '/pages/circle/circle'
					})
				}
			},
			// 授权接口

			async getList() {
				let {
					data,
					code
				} = await this.$post('/backend/v1/vxLogin', this.userinfo, '')
				if (code = 200) {
					uni.setStorageSync("openId", data.openid)
					uni.setStorageSync("sessionKey", data.sessionKey)
					uni.setStorageSync("token", data.token)
					uni.showToast({
						icon: 'success',
						title: '授权成功',
						duration: 2300
					})
					this.stauts = true
				}


				let {
					data: info
				} = await this.$get('/user/UserInfo')
				uni.setStorageSync('anonymousAvatar', info.anonymousAvatar)
				uni.setStorageSync('company', info.company)
				uni.setStorageSync('flowerName', info.flowerName)
				uni.setStorageSync('userId', info.userId)
				uni.setStorageSync('position', info.position)
				uni.setStorageSync('userId', info.userId)
				let {
					data: jianli
				} = await this.$get('/userResumeFile/list')
				if (jianli) {
					uni.setStorageSync('jianli', true)
				}
			}

		}
	}
</script>

<style lang="scss">
	.page {
		width: 100vw;
		height: 100vh;
		padding-top: 250rpx;
		._img {
			image{
			width: 500rpx;
			height: 150rpx;
			margin: 146rpx auto 0;
			}
			
		}
	
		.title {
			width: 340rpx;
			font-size: 40rpx;
			font-weight: 600;
			//text-shadow: 0 1rpx 0 rgba(0, 0, 0, 1), 0 6rpx 0 rgba(0, 0, 0, 0.5);
			margin: 39rpx auto 0;
		}
	
		.authorization {
			button {
				width: 560rpx;
				height: 90rpx;
				line-height: 90rpx;
				background-color: #0265EB;
				background-blend-mode: normal,
					normal;
				border-radius: 45rpx;
				margin: 120rpx auto 0;
	
				.botton {
					font-size: 36rpx;
					letter-spacing: 2rpx;
					color: #ffffff;
				}
			}
		}
	
		.skip {
			width: 560rpx;
			height: 90rpx;
			line-height: 90rpx;
			background:  #cccccc;
			background-blend-mode: normal,
				normal;
			border-radius: 45rpx;
			margin: 30rpx auto 0;
	
			.skip-item {
				font-size: 36rpx;
				letter-spacing: 2rpx;
				color: #999999;
			}
		}
	}

</style>
